#include "widget.h"
#include "ui_widget.h"



/*
void Widget::ouvrir_recentBase()
{
    ouvrir_base(recentFileAction->toolTip());
}
*/

void Widget::ouvrir_base(){

        QSettings s;
        QString hname = s.value("connexion/hostname").toString();
        QString dbname = s.value("connexion/dbname").toString();
        QString user = s.value("connexion/user").toString();
        QString pw = s.value("connexion/pw").toString();

        //On ferme une hypothétique base déjà ouverte
        QSqlDatabase::database().close();
        QSqlDatabase::removeDatabase(QSqlDatabase::database().connectionName());
        //On créer la connexion avec les infos correspondantes
        db = QSqlDatabase::addDatabase("QMYSQL");
        db.setHostName(hname);
        db.setDatabaseName(dbname);
        db.setUserName(user);
        db.setPassword(pw);
        //On ouvre la base
        if(db.open()){
            //completer_structure(db);
        } else {
            //Si la base ne peut pas s'ouvrir on affiche une erreur
            QMessageBox::warning(this,tr("Nom de la base"), tr("La base ne veut pas s'ouvrir! Pas de chance :D !!!"));
        }

}



void Widget::fermer_base()  {
    //On détruit la connexion existante
    QSqlDatabase::database().close();
    QSqlDatabase::removeDatabase(QSqlDatabase::database().connectionName());
}

void Widget::majListProms(QString fam) {
    //On nettoie la liste
    ui->listWidget->clear();

    QStringList l = Pg::listProms(fam.toInt());
    if(!tactile){
        for(int i =0; i<l.count(); i++){
            l.replace(i,"[F"+QString::number(i+1)+"]-"+l.at(i));
        }
    }
    ui->listWidget->addItems(l);

}

void Widget::majTextEdits(){

    QSettings set;
    prefsBlocDroite = set.value("prefBlocDroite").toString().split(";");
    prefsBlocGauche = set.value("prefBlocGauche").toString().split(";");


    QString s = "";
    for(int i=0; i < prefsBlocDroite.size(); i++){
        s += ModuleAccueil::toHtml(prefsBlocDroite.at(i));
    }

    ui->textEdit->setHtml(s);

    QString s2 = "";
    for(int i=0; i < prefsBlocGauche.size(); i++){
        s2 += ModuleAccueil::toHtml(prefsBlocGauche.at(i));
    }

    QSettings settings;
    ui->textEdit_2->setHtml(s2 + settings.value("message").toString());

}


void Widget::completer_structure(QSqlDatabase DB){
    QStringList table_list = DB.tables(QSql::Tables);
    //QMessageBox::information(this,"Tables",table_list.join(" ; "));

    if (!table_list.contains("Pg"))
    // créer la table Pg
    {
        QSqlDatabase::database().transaction();
        QSqlQuery myQuery;
        // creation de la table
        myQuery.prepare("CREATE TABLE IF NOT EXISTS `Pg` (`Id` smallint(6) NOT NULL AUTO_INCREMENT, `Fams` tinyint(4) unsigned NOT NULL, `Centre` varchar(2) NOT NULL, `Proms` tinyint(3) unsigned NOT NULL, `Bucque` varchar(30) NOT NULL, `Actif` tinyint(1) NOT NULL DEFAULT '0', `Statut` tinyint(1) NOT NULL DEFAULT '0', `Solde` decimal(5,2) NOT NULL DEFAULT '0.00', PRIMARY KEY (`Id`), UNIQUE KEY `gadz` (`Fams`,`Centre`,`Proms`,`Statut`)) ENGINE=InnoDB  DEFAULT CHARSET=latin1");
        myQuery.exec();
        QSqlDatabase::database().commit();

    }

    if (!table_list.contains("Biroute"))
    {
        // créer la table Biroute
        QSqlDatabase::database().transaction();
        QSqlQuery myQuery;
        // creation de la table
        myQuery.prepare("CREATE TABLE IF NOT EXISTS `Biroute` (`Id` mediumint(9) NOT NULL AUTO_INCREMENT,`Conso` mediumint(9) NOT NULL,`Quantite` smallint(6) NOT NULL DEFAULT '0',`Date` datetime NOT NULL,PRIMARY KEY (`Id`)) ENGINE=InnoDB  DEFAULT CHARSET=latin1");
        myQuery.exec();
        QSqlDatabase::database().commit();

    }
    if (!table_list.contains("Historique"))
    {
        // créer la table Historique
        QSqlDatabase::database().transaction();
        QSqlQuery myQuery;
        // creation de la table
        myQuery.prepare("CREATE TABLE IF NOT EXISTS `Historique` (`Id` int(11) NOT NULL AUTO_INCREMENT,`Pg` smallint(6) NOT NULL,`Conso` mediumint(9) NOT NULL,`Date` datetime NOT NULL,PRIMARY KEY (`Id`)) ENGINE=InnoDB  DEFAULT CHARSET=latin1");
        myQuery.exec();
        QSqlDatabase::database().commit();

    }
    if (!table_list.contains("Consos"))
    {
        // créer la table Consos
        QSqlDatabase::database().transaction();
        QSqlQuery myQuery;
        // creation de la table
        myQuery.prepare("CREATE TABLE IF NOT EXISTS `Consos` (`Id` mediumint(9) NOT NULL AUTO_INCREMENT,`Intitule` varchar(30) NOT NULL,`MotsClefs` text,`Actif` tinyint(1) NOT NULL,`Prix` decimal(4,2) unsigned NOT NULL,`Dispo` smallint(6) NOT NULL,`Reserve` smallint(6) NOT NULL,PRIMARY KEY (`Id`)) ENGINE=InnoDB  DEFAULT CHARSET=latin1");
        myQuery.exec();
        QSqlDatabase::database().commit();

    }
    if (!table_list.contains("Remise"))
    {
        // créer la table Remise
        QSqlDatabase::database().transaction();
        QSqlQuery myQuery;
        // creation de la table
        myQuery.prepare("CREATE TABLE IF NOT EXISTS `Remise` (`Id` mediumint(9) NOT NULL AUTO_INCREMENT, `Pg` smallint(6) NOT NULL,`Somme` decimal(5,2) NOT NULL,`Zident` smallint(6) NOT NULL,`Num` mediumint(7) unsigned zerofill NOT NULL,`Date` datetime NOT NULL,PRIMARY KEY (`Id`)) ENGINE=InnoDB  DEFAULT CHARSET=latin1");
        myQuery.exec();
        QSqlDatabase::database().commit();

    }
}

